import Vue from 'vue'
import Vuex from 'vuex'
import {
	tbDictDefinitionQuery,
	tbDictDefinitionQueryConstantStrList,
	tbDictDefinitionUpdateByConstantCode,
	tbDictTbSysDictDetail,
	openApiTbDictDefinitionQueryConstantStrList,
	tbDictDefinitionQueryEnumList,
	addTbDictDefinitionEnumListClidren,
	deleteTbDictDefinitionEnumListClidren,
	sysJournalCollectAdd,
	tbSysDictDetailQueryByCode
} from '@/api/admin/system/dictionary'
Vue.use(Vuex)

const state = {}

const getters = {}

const actions = {
	//通过code取数据字典的值(前台页面调用)
	async getDictionaryVal({ commit }, code) {
		let res = await openApiTbDictDefinitionQueryConstantStrList({ code })
		return res.data.code == 0 ?
			res.data.data :
			Promise.reject(new Error('faile'))
	},
	//通过code取数据字典的值(后台页面调用)
	async getDictByCode({ commit }, code) {
		let res = await tbDictDefinitionQueryConstantStrList({ code })
		return res.data.code == 0 ?
			res.data.data[0] :
			Promise.reject(new Error('faile'))
	},
	//保存数据字典信息
	async saveDicByCode({ commit }, { code, name }) {
		if (!name) return
		let res = await tbDictDefinitionUpdateByConstantCode({ code, name })
		return res.data.code == 0 ? 'ok' : Promise.reject(new Error('faile'))
	},
	//通过code取数据字典枚举的值(后台页面调用)
	async getDictionaryEnumListVal({ commit }, code) {
		let res = await tbDictDefinitionQueryEnumList({ code })
		return res.data.code == 0 ? res.data.data : Promise.reject(new Error('faile'))
	},
	//通过code取数据字典枚举id(后台页面调用)
	async getDictionaryEnumListIdByCode({ commit }, code) {
		let res = await tbDictDefinitionQuery({ code });
		return (res.data.code == 0 && res.data.data[0]) ? res.data.data[0].id : Promise.reject(new Error('faile'))
	},
	//通过id查询枚举子集列表(后台页面调用)
	async getDictionaryEnumListChildrenById({ commit }, dictId) {
		let res = await tbDictTbSysDictDetail({ dictId });
		return res.data.code == 0 ? res.data.data : Promise.reject(new Error('faile'))
	},
	//通过code查询枚举子集列表(后台页面调用)
	async tbSysDictDetailQueryByCodes({ commit }, dictId) {
		let res = await tbSysDictDetailQueryByCode({ dictId });
		return res.data.code == 0 ? res.data.data : Promise.reject(new Error('faile'))
	},
	//添加数据字典枚举数据(后台页面调用)
	async saveDicChildren({ commit }, data) {
		let res = await addTbDictDefinitionEnumListClidren(data);
		return res.data.code == 0 ? 'ok' : Promise.reject(new Error('faile'))
	},
	//删除一条数据字典枚举数据
	async deleteDicChildren({ commit }, id) {
		let res = await deleteTbDictDefinitionEnumListClidren(id);
		return res.data.code == 0 ? 'ok' : Promise.reject(new Error('faile'))
	},
	//收藏下载数量
	async sysJournalCollectAdd({ commit }, id) {
		let res = await sysJournalCollectAdd(id);
		return res.data.code == 0 ? 'ok' : Promise.reject(new Error('faile'))
	}
}

const mutations = {}

export default {
	state,
	getters,
	actions,
	mutations,
}